package com.jevons.geo_edit.dialog;

import android.content.Context;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

import com.jevons.geo_edit.R;
import com.jevons.geo_edit.constant.GisGeometryType;
import com.jevons.geo_edit.databinding.DialogCreateGeometryLayerBinding;
import com.jevons.muffin.utilcode.StringUtils;
import com.jevons.muffin.widget.CustomToast;

/**
 * Copyright 2020 JxGIS
 *
 * @file CreateGeoLayerDialog
 * @auth linaisheng
 * Created on 2023/6/14.
 * Description：
 */
public class CreateGisLayerDialog extends AlertDialog {


    private DialogCreateGeometryLayerBinding dialogBinding;

    private OnCreateLayerListener listener;

    public CreateGisLayerDialog(
            @NonNull Context context, OnCreateLayerListener createListener) {
        super(context, R.style.CustomDialog);

        dialogBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                getLayoutRes(), null, false);
        setView(dialogBinding.getRoot());
        listener = createListener;
        initView();
    }


    private int getLayoutRes() {
        return R.layout.dialog_create_geometry_layer;
    }

    private void initView() {
        dialogBinding.btnCancel.setOnClickListener(view -> {
            dismiss();
        });
        dialogBinding.btnConfirm.setOnClickListener(view -> {
            if (StringUtils.isEmpty(dialogBinding.etName.getText().toString())) {
                CustomToast.shortShow("请输入图层名称");
            } else {
                createGeoLayer();
                dismiss();
            }
        });
    }

    private void createGeoLayer() {
        GisGeometryType geoType;
        if (dialogBinding.rgChooseGeometry.getCheckedRadioButtonId() == R.id.rb_geometry_point) {
            geoType = GisGeometryType.Point;
        } else if (dialogBinding.rgChooseGeometry.getCheckedRadioButtonId() == R.id.rb_geometry_line) {
            geoType = GisGeometryType.Line;
        } else {
            geoType = GisGeometryType.Polygon;
        }

        if (listener != null) {
            listener.onCreate(geoType, dialogBinding.etName.getText().toString());
        }
    }

    public interface OnCreateLayerListener {

        void onCreate(GisGeometryType geoType, String layerName);

    }

}
